//
//  ZFLTabbarViewController.m
//  iOSAppDevRepo_Example
//
//  Created by 翁胜琼 on 2019/2/12.
//  Copyright © 2019 折疯了. All rights reserved.
//

#import "ZFLTabbarViewController.h"
#import "ZFLNavigationController.h"
#import <WSQBaseTabbarModel.h>
#import <UIImage+Color.h>
#import "ZFLTabbarBackgroundView.h"

@interface ZFLTabbarViewController ()

@property (copy,nonatomic,readwrite) NSArray<WSQBaseTabbarModel *> * models;

@end

@implementation ZFLTabbarViewController
@synthesize models = _models;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupTabbar];
}

- (void)setupTabbar
{
    self.tabBar.tintColor = UICOLORFROMRGB(35, 215, 127);
    
    self.tabBar.backgroundColor =[UIColor clearColor];
    [self.tabBar setBackgroundImage:[UIImage sq_imageWithColor:UICOLORFROMRGBA(1,1,1,0)]];
    [self.tabBar setShadowImage:[UIImage new]];
    
    //   设置为半透明，目的是将 tabbar 中 backgroundView透明度设置为0
    self.tabBar.translucent = YES;
    [self setupCenterBtn];
//    [self setupShapeLayer];
    [self setupBackgroundShapeView];
}

// 为 tabbar 添加自定义绘制视图，因为直接添加 layer 无效
- (void)setupBackgroundShapeView
{
    ZFLTabbarBackgroundView *view = [[ZFLTabbarBackgroundView alloc] initWithFrame:self.tabBar.bounds];
    // 必须要手动设置视图背景颜色，否则默认是黑色
    view.backgroundColor = clear_color;
    [self.tabBar addSubview:view];
    [self.tabBar sendSubviewToBack:view];
}

- (void)setupCenterBtn
{
    UIButton *centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [centerBtn setImage:[UIImage imageNamed:@"Tabbar_Icon_3_Selected"] forState:UIControlStateNormal];
    [centerBtn sizeToFit];
    centerBtn.center = CGPointMake(self.tabBar.center.x, 35);
    [self.tabBar addSubview:centerBtn];
}

- (void)addChildViewControllerWithModels:(NSArray<WSQBaseTabbarModel *> *)models
{
    _models = [models copy];
    
    __weak typeof(self) weakSelf = self;
    [models enumerateObjectsUsingBlock:^(WSQBaseTabbarModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        __strong typeof(weakSelf) self = weakSelf;
        
        UIViewController *vc;
        
        vc = [NSClassFromString(obj.vc) new];
        vc.navigationItem.title = obj.title;
        ZFLNavigationController *nav = [[ZFLNavigationController alloc] initWithRootViewController:vc];
        
        nav.tabBarItem.image = [[UIImage imageNamed:obj.normalImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        nav.tabBarItem.selectedImage = [[UIImage imageNamed:obj.selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        nav.tabBarItem.title = obj.title;
        
        [nav.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
        
        [self addChildViewController:nav];
        
    }];
    
}

@end
